$(function () {
    $("#jqGrid").jqGrid({
                            url: baseURL + 'script/list',
                            datatype: "json",
                            colModel: [
                                {
                                    label: 'scriptId',
                                    name: 'scriptId',
                                    hidden: true,
                                    index: 'script_id',
                                    key: true
                                },
                                {label: '脚本编号', name: 'scriptNo', index: 'script_no', width: 150},
                                {
                                    label: '脚本名称',
                                    name: 'scriptName',
                                    index: 'script_name',
                                    width: 80
                                },
                                {
                                    label: '脚本版本',
                                    name: 'scriptVersion',
                                    index: 'script_version',
                                    width: 50
                                },
                                {label: '访问token', name: 'accessToken', index: 'access_token', width: 100},
                                {
                                    label: '创建时间',
                                    name: 'createTime',
                                    index: 'create_time',
                                    width: 80
                                },
                                {
                                    label: '创建人',
                                    name: 'createUserName',
                                    index: 'createUserName',
                                    width: 80
                                },
                                {
                                    label: '状态',
                                    name: 'status',
                                    index: 'status',
                                    width: 40,
                                    formatter: function (value, options, row) {
                                        return value === 1
                                            ? '<span class="label label-danger">禁用</span>'
                                            : '<span class="label label-success">正常</span>';
                                    }
                                }
                            ],
                            viewrecords: true,
                            height: 385,
                            rowNum: 10,
                            rowList: [10, 30, 50],
                            rownumbers: true,
                            rownumWidth: 25,
                            autowidth: true,
                            multiselect: true,
                            pager: "#jqGridPager",
                            jsonReader: {
                                root: "page.list",
                                page: "page.currPage",
                                total: "page.totalPage",
                                records: "page.totalCount"
                            },
                            prmNames: {
                                page: "page",
                                rows: "limit",
                                order: "order"
                            },
                            gridComplete: function () {
                                //隐藏grid底部滚动条
                                // $("#jqGrid").closest(".ui-jqgrid-bdiv")
                                //   .css({"overflow-x": "hidden"});
                            }
                        });
});

var vm = new Vue({
                     el: '#rrapp',
                     data: {
                         q:{
                             scriptName:""
                         },
                         showList: true,
                         title: null,
                         script: {},
                         appList:{}
                     },
                     methods: {
                         query: function () {
                             vm.reload();
                         },
                         add: function () {
                             vm.showList = false;
                             vm.title = "新增";
                             vm.script = {};
                             this.getAppList();
                         },
                         update: function (event) {
                             var scriptId = getSelectedRow();
                             if (scriptId == null) {
                                 return;
                             }
                             vm.showList = false;
                             vm.title = "修改";

                             vm.getInfo(scriptId)
                         },
                         saveOrUpdate: function (event) {
                             var url = vm.script.scriptId == null ? "script/save" : "script/update";
                             $.ajax({
                                        type: "POST",
                                        url: baseURL + url,
                                        contentType: "application/json",
                                        data: JSON.stringify(vm.script),
                                        success: function (r) {
                                            if (r.code === 0) {
                                                alert('操作成功', function (index) {
                                                    vm.reload();
                                                });
                                            } else {
                                                alert(r.msg);
                                            }
                                        }
                                    });
                         },
                         del: function (event) {
                             var scriptIds = getSelectedRows();
                             if (scriptIds == null) {
                                 return;
                             }

                             confirm('确定要删除选中的记录？', function () {
                                 $.ajax({
                                            type: "POST",
                                            url: baseURL + "script/delete",
                                            contentType: "application/json",
                                            data: JSON.stringify(scriptIds),
                                            success: function (r) {
                                                if (r.code == 0) {
                                                    alert('操作成功', function (index) {
                                                        $("#jqGrid").trigger("reloadGrid");
                                                    });
                                                } else {
                                                    alert(r.msg);
                                                }
                                            }
                                        });
                             });
                         },
                         getInfo: function (scriptId) {
                             $.get(baseURL + "script/info/" + scriptId, function (r) {
                                 vm.script = r.script;
                             });
                         },
                         getAppList: function(){
                             $.get(baseURL + "script/selectApp", function(r){
                                 vm.appList = r.list;
                             });
                         },
                         reload: function (event) {
                             vm.showList = true;
                             var page = $("#jqGrid").jqGrid('getGridParam', 'page');
                             $("#jqGrid").jqGrid('setGridParam', {
                                 page: page,
                                 postData:{'scriptName':vm.q.scriptName}
                             }).trigger("reloadGrid");
                         }
                     }
                 });